﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using WPFExcelReport.Helpers;
using System.Data.OracleClient;
using System.IO;
using INTEX.DataBase.Tools;
using System.Linq;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        private void YCAEK03PO(DataSet db)
        {
            try
            {
                EKKWE1 = 0;
                EKKME1 = 0;
                if (!db.Tables.Contains("EKKOPF"))
                    return;
                DataRow dwEKK = db.Tables["EKKOPF"].Rows[0];
                string value = "";
                Dictionary<string, int> colorpos = new Dictionary<string, int>();
                int x = 1, y = 1;
                x = 5; y = 18;
                //exAgent.SetCellsPicture(5, 17, @"D:\DS40\A1.jpg"); 
                value = dwEKK["EKKAUFKRS"].ToString() + "-" + dwEKK["EKKAUFLFD"].ToString();
                exAgent.SetCellValue(x, y, value);
                x = 3; y = 30;
                exAgent.SetCellValue(x, y, date);
                int rown = FillInEKPOS();
                FillinSing(db, 2260);
                FillInQPPlan(2106);
                exAgent.DeleteRows(rown, 2100);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillinSing(DataSet db, int rown = 24, int fixednum = 1)
        {
            int x = 24, y = 1;
            x = rown; y = 1;

            DataRow ekk = dsSource.Tables["EKKOPF"].Rows[0];
            string liefnr = ekk["EKKLIEFNR"].ToString();
            string kond = ekk["EKKKONDSL"].ToString();
            string payment = "";
            var qy = from DataRow rx in dsSource.Tables["LIEFKO"].AsEnumerable()
                     where rx["LSKLIEFNR"].ToString() == liefnr
                     && rx["LSKKONDSL"].ToString().Trim() == kond.Trim()
                     select rx;

            foreach (DataRow row in qy)
            {     //LSKBANKBEZ	LSKBANKORT LSKIBAN
                x = rown + 4; y = 21;
                exAgent.SetCellValue(x, 18, "开户行：");
                exAgent.SetCellValue(x, 21, GetGB2312Value(row["LSKBANKORT"].ToString().Trim()));
                exAgent.SetCellValue(x, 1, "开户行：");
                exAgent.SetCellValue(x, 4, "上海银行胶州路支行");
                x = rown + 5; y = 21;
                exAgent.SetCellValue(x, 1, "账户：");
                //exAgent.SetCellValue(x, 4, "315914-03000129533");
                exAgent.SetCellValue(x, 18, "账户：");
                exAgent.SetCellValue(x, 21, GetGB2312Value(row["LSKBANKBEZ"].ToString().Trim()));
                payment = row["LSKZAHLBED"].ToString();
                x = rown + 6; y = 21;
                exAgent.SetCellValue(x, 1, "帐号：");
                exAgent.SetCellValue(x, 4, "315914-03000129533");
                exAgent.SetCellValue(x, 18, "帐号：");
                exAgent.SetCellValue(x, 21, GetGB2312Value(row["LSKIBAN"].ToString().Trim()));
                payment = row["LSKZAHLBED"].ToString();

            }
            if (db.Tables.Contains("ZABED"))
            {

                foreach (DataRow rx in db.Tables["ZABED"].Rows)
                {
                    payment += " " + GetGB2312Value(rx["ZABBEZ"].ToString());
                    break;
                } 
                exAgent.SetCellValue(9, 5, payment);
            }
            qy = from DataRow rx in dsSource.Tables["LIEFDAT"].AsEnumerable()
                 where rx["LSLIEFNR"].ToString() == liefnr
                 select rx;

            foreach (DataRow row in qy)
            {
                x = 4; y = 4;
                exAgent.SetCellValue(x, y, GetGB2312Value(row["LSNAME1"].ToString().Trim()));
                x = rown + 3; y = 21;
                exAgent.SetCellValue(x, 18, "乙方：");
                exAgent.SetCellValue(x, 21, GetGB2312Value(row["LSNAME1"].ToString().Trim()));
                x = rown + 7;
                exAgent.SetCellValue(x, 1, "地址：");
                exAgent.SetCellValue(x, 4, "上海普陀区金沙江路1340弄57号D区4D栋4层");
                exAgent.SetCellValue(x, 18, "地址：");
                exAgent.SetCellValue(x, 21, GetGB2312Value(row["LSSTRASSE1"].ToString().Trim()));
            }
            x = rown + 3;
            exAgent.SetCellValue(x, 1, "甲方：");
            exAgent.SetCellValue(x, 4, "上海布景服饰有限公司");
            x = rown + 8;
            exAgent.SetCellValue(x, 1, "法定代表人或委托代理人：");
            exAgent.SetCellValue(x, 18, "法定代表人或委托代理人：");
            x = rown + 9;
            exAgent.SetCellValue(x, 1, "联系人：");
            exAgent.SetCellValue(x, 18, "联系人：");
        }
        private void FillInQPPlan(int rown = 2106)
        {
            int x = 24, y = 1;
            x = rown; y = 1;
            string value = "";
            DataRow ekp = dsSource.Tables["ICC_EKPOS"].Rows[0];
            string art = ekp["EKPART"].ToString();
            var qy = from DataRow rx in dsSource.Tables["WAPROF"].AsEnumerable()
                     where rx["WAPART"].ToString() == art
                     select rx;
            foreach (DataRow row in qy)
            {
                string pos = Helper.GetSubRollNum(row["WAPPOS"].ToString());
                value = row["WAPBEM"].ToString();
                if (value.Trim() == "")
                    value = "0";
                string std = (System.Convert.ToDecimal(value) / 10000).ToString();
                value = row["WAPMIN"].ToString();
                if (value.Trim() == "")
                    value = "0";
                string min = (System.Convert.ToDecimal(value) / 1000).ToString();
                value = row["WAPMAX"].ToString();
                if (value.Trim() == "")
                    value = "0";
                string max = (System.Convert.ToDecimal(value) / 1000).ToString();
                string um = row["WAPME"].ToString();
                exAgent.SetCellValue(x, y, pos);

                exAgent.SetCellValue(x, 3, GetGB2312Value(row["WAPANFART"].ToString() + ":" + row["QPMKURZB"].ToString().Trim()));
                exAgent.SetCellValue(x, 10, min + "~" + max + um);
                exAgent.SetCellValue(x, 17, std);
                x++;
            }
        }

    }
}